package cn.chenwx.bbsforum.mapper;

import cn.chenwx.bbsforum.entity.Article;
import cn.chenwx.bbsforum.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
@Mapper
@Repository
public interface ArticleMapper {

    // 查询所有的文章

    @Select("select * from article")
    @Results(
            id = "ArticleMap",value = {
        @Result(id = true,property = "id",column = "id"),
        @Result(property = "title",column = "title"),
        @Result(property = "content",column = "content"),
        @Result(property = "tag",column = "tag"),
        @Result(property = "status",column = "status"),
        @Result(property = "creatTime",column = "creat_time"),
        @Result(property = "modifyTime",column = "modify_time"),
        @Result(property = "userId",column = "user_id"),
        @Result(property = "commentsNum",column = "comments_num")
//        @Result(property = "user",column = "user_id",javaType = User.class,one = @One(select = "cn.chenwx.bbsforum.mapper.UserMapper.findUserById"))
    })
    public List<Article> findAllArticle();
    // 通过id查询一个文章
    @Select("select * from article where id=#{id}")
    @Results(id = "article",
            value={
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "creatTime", column = "creat_time"),
            @Result(property = "modifyTime", column = "modify_time"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "commentsNum", column = "comments_num"),
            @Result(property = "creatTimeStr",column = "time_str")

    })
   public Article findArticleById(@Param("id")int id);
    // 增加一个文章
    @Insert("insert into article(title,content,tag,user_id) values(#{article.title},#{article.content},#{article.tag},#{article.userId})")
   public int addArticle(@Param("article") Article article);
    @Update("update article set title=#{title},content=#{content},status=#{status},tag=#{tag} where id=#{id}")
   public int modifyArticle(Article article);
   @Delete("delete from article where id=#{id}")
   public int deleteArticle(@Param("id") int id);

    @Select("select username form user where id=#{authorId}")
    String findArticleAuthor (@Param("authorId") int userId);
    @Select("select count(1) from article")
    int getArticleNum();

    @Select("select * from article where tag=#{tagName}")
    @Results(
          {
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "title",column = "title"),
            @Result(property = "content",column = "content"),
            @Result(property = "tag",column = "tag"),
            @Result(property = "status",column = "status"),
            @Result(property = "creatTime",column = "creat_time"),
            @Result(property = "modifyTime",column = "modify_time"),
            @Result(property = "userId",column = "user_id"),
            @Result(property = "commentsNum",column = "comments_num"),

    })
    List<Article> findAritcleByTag(@Param("tagName") String tagName);
    @Select("select * from article where user_id = #{id}")
    @ResultMap("article")
    Article findArticleByUserId(@Param("id") Integer id);
    @Select("select user_id where id=#{id} ")
    int findUserId(@Param("id") int id);
}
